package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.UserRoleService;
import com.tzbank.entity.backstage.UserRole;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class UserRoleController {

	@Autowired
	private UserRoleService userRoleService;

	/**
	 * 添加用户关联角色
	 * @param request
	 * @param userRole
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "insertUserRole")
	public Map<String, Object> insertUserRole(HttpServletRequest request, @RequestBody UserRole userRole) {
		try {
			return this.userRoleService.insertUserRole(userRole);
		} catch (Exception e) {
			log.error("UserRoleController insertUserRole error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 修改用户关联角色
	 * @param request
	 * @param userRole
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "updateUserRole")
	public Map<String, Object> updateUserRole(HttpServletRequest request, @RequestBody UserRole userRole) {
		try {
			return this.userRoleService.updateUserRole(userRole);
		} catch (Exception e) {
			log.error("UserRoleController updateUserRole error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 删除用户关联角色
	 * @param request
	 * @param userRole
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@PostMapping(value = "deleteUserRole")
	public Map<String, Object> deleteUserRole(HttpServletRequest request, @RequestBody UserRole userRole) {
		try {
			return this.userRoleService.deleteUserRole(userRole);
		} catch (Exception e) {
			log.error("UserRoleController deleteUserRole error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 通过自增主键查询用户关联角色
	 * @param request
	 * @param autoId
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectUserRoleByAutoId")
	public Map<String, Object> selectUserRoleByAutoId(HttpServletRequest request,
			@RequestParam(required = true) Integer autoId) {
		try {
			return this.userRoleService.selectUserRoleByAutoId(autoId);
		} catch (Exception e) {
			log.error("UserRoleController selectUserRoleByAutoId error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	/**
	 * 查询用户关联角色
	 * @param request
	 * @return Map<String, Object>
	 * @author XUZONGTIAN 2018年6月1日
	 */
	@GetMapping(value = "selectUserRoleList")
	public Map<String, Object> selectUserRoleList(HttpServletRequest request) {
		try {
			return this.userRoleService.selectUserRoleList();
		} catch (Exception e) {
			log.error("UserRoleController selectUserRoleList error:", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

}
